#################################### Appendix (F) ##############################################
################################################################################################

# Load the required packages
rm(list = ls())
library(fixest)
library(readstata13)
library(collapse)
library(dplyr)
library(stdidx)


#Loading the main dataset: 
# data_2rounds: Contains data on the 2 rounds of the election, excluding the pre-electoral period 
# data_3rounds: Contains data on the 2 rounds of the election, including the pre-electoral period as an additional round

data <- read_dta("data_2rounds.dta")
data3 <- read_dta("data_3rounds.dta")

## Aggregate the data, then merge the clientelism data
agg_data <- collap(data, allintimid+mb_dummy + pct_urban+pct_emp + sdeduscore  + logprotest +
                  logregistered+female_pct+round2+incumbent+nocand_competing+newndp+split_ndp ~ governorate+district_name, list(fsum, fmean))

clientdata <- read_dta("clientelismdata.dta")
agg_data <- merge(agg_data, clientdata, by=c("district_name"))

# Define two variables needed for analysis: (1) Dummy for MB presence, (2) Dummy for reported vote-buying
agg_data$mb_dummy <- as.numeric(agg_data$fmean.mb_dummy>0)
agg_data$votebuyingd <- as.numeric(agg_data$votebuying>0)

#Create dictionary to present the names of the variables in the tables
dict=c("mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
       "sdeduscore"="Education (sd)","eduscore2"="Education2 (sd)", "gov_employed"="Gov. Employment (%)", "logprotest"="Protest (log)", 
       "highprofile"="High Profile", "logregistered"="Registered (log)", "female_pct"="Female (%)", "round2"="Runoff", 
       "votebuyingd"="Vote-buying", "fmean.female_pct"="Female", "fmean.logregistered"="Registered (log)", 
       "fmean.pct_urban"="Urban (%)", "fmean.pct_emp"="Employment (%)", "fmean.sdeduscore"="Education (sd)", 
       "fmean.gov_employed"="Gov. Employment (%)", "newndp"="New NDP", "split_ndp"="NDP Dissidents", 
       "incumbent"="Incumbents", "nocand_competing"="Candidates No.", "death_ndpaffil"="NDP Death", "client_index"="Clientelism", 
       "watani_1984" = "NDP 1984 (%)", "wafd_1984" = "MB Coalition 1984 (%)", "fmean.incumbent"="Incumbents", "fmean.nocand_competing"="Candidates No.", 
       "fmean.newndp"="New NDP", "fmean.split_ndp"="NDP Dissidents", "fmean.logprotest"="Protest (log)")


###### Figure (3)

temp <- subset(agg_data, is.na(agg_data$price)==FALSE)

ggplot(agg_data, aes(x=price, y=fsum.allintimid))+
  geom_point(color="black", size=4, shape=1)+
  geom_smooth(method=lm, se=FALSE, col="black")+
  labs(x = "Maximum Price of the Vote (in Egyptian Pounds)",
       y = "Electoral Violence (Total)")+
  theme_bw()+theme(axis.text=element_text(size=10),
                   axis.title=element_text(size=13,face="bold"))+ylim(0,12)


####### Table (15)

#Define log of total violence
data$logallintimid <- log(data$allintimid+1)

# Analysis
main1 <- feols(turnout_pct ~  pct_urban+pct_emp + sdeduscore + mb_dummy + newndp +  split_ndp + logprotest +female_pct+round2 + incumbent + nocand_competing|governorate, 
               data)

main2 <- feols(turnout_pct ~  pct_urban+pct_emp + sdeduscore + mb_dummy + newndp +  split_ndp + logprotest +female_pct+round2 + incumbent + nocand_competing + logallintimid |governorate, 
               data)

etable(main1, main2,  se = c("cluster"), cluster=~governorate,  digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), interaction.combine = "*",  dict=dict)





####### Table (16)

#For this analysis, we first introduce a survey-based measure for clientelism at the district level 

survey_clientdat <- read_dta("survey_client.dta")

#We will merge it to both the aggregated data, and the 2-round dataset
agg_data <- merge(agg_data, survey_clientdat, by=c("district_name"))
data <- merge(data, survey_clientdat, by=c("district_name"))

#Define a dummy variable for Muslim Brotherhood
agg_data$mb_dummy <- as.numeric(agg_data$fmean.mb_dummy>0)


#Then, we run the analysis for Table (16)
m1 <- feols(sdclient_freq ~ votebuyingd | governorate, 
            subset(agg_data))

m2 <- feols(sdclient_freq ~ votebuyingd+fmean.pct_urban + fmean.pct_emp + fmean.sdeduscore | governorate, 
            subset(agg_data))


etable(m1, m2,  se = c("cluster"), cluster=~governorate,  digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), interaction.combine = "*",  dict=dict)



######## Table (17)

main1 <- fenegbin(allintimid ~ sdclient_freq | governorate, data)           

main2 <- fenegbin(allintimid ~ sdclient_freq+newndp +  split_ndp + mb_dummy +   + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

main3 <- fenegbin(allintimid ~ sdclient_freq+newndp +  split_ndp + mb_dummy + pct_emp + sdeduscore + pct_urban   + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data)

m1 <- fenegbin(fsum.allintimid ~ sdclient_freq| governorate, 
               agg_data)

m2 <- fenegbin(fsum.allintimid ~ sdclient_freq + mb_dummy+fmean.newndp+fmean.split_ndp+
                 fmean.logprotest+ fmean.female_pct + fmean.logregistered +fmean.incumbent+fmean.nocand_competing| governorate, 
               agg_data)

m3 <- fenegbin(fsum.allintimid ~ sdclient_freq + mb_dummy+fmean.newndp+fmean.split_ndp++ fmean.pct_urban + fmean.pct_emp + fmean.sdeduscore+
                 fmean.logprotest+ fmean.female_pct + fmean.logregistered +fmean.incumbent+fmean.nocand_competing| governorate, 
               agg_data)

etable(main1, main2, main3, m1, m2, m3, se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")



